<?
class Controller_Plugin_Log extends Zend_Controller_Plugin_Abstract{
	
	protected $_log = null;
	
	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
		
		$_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
		
		$writer = new Zend_Log_Writer_Db($_adapter, 'log');
		
		$this->_log = new Log($writer);
                
                try{
                    $user = new VkUser();
                } catch(Zend_Service_Exception $e) {
                    $user = null;
                }
                
                $this->_log->setUser($user);
		
		Zend_Registry::set('Log', $this->_log);
		
		if($request->getActionName() != 'log'){
		
			$_params['uri'] = $_SERVER['REQUEST_URI'];
		
			ob_start();
				if($_GET){
					print_r($_GET);
				}
			$_params['get'] = ob_get_contents(); ob_end_clean();
			
			ob_start();
				if($_POST){
					print_r($_POST);
				}
			$_params['post'] = ob_get_contents(); ob_end_clean();
		
			$this->_log->info('Start: URI('.$_params['uri'].'); GET('.$_params['get'].'); POST('.$_params['post'].')');
		}
	}
	
	public function dispatchLoopShutdown(){
		if($this->getRequest()->getActionName() != 'log'){
			$this->_log->info('Stop: RESPONSE_CODE('.$this->_response->getHttpResponseCode().')');
		}
	}
	
}